//package com.xiguaji.tiktok.task.market.event;
//
//import com.google.common.eventbus.AllowConcurrentEvents;
//import com.google.common.eventbus.Subscribe;
//import com.xiguaji.boot.eventbus.Event;
//import com.xiguaji.boot.eventbus.IEventListener;
//import com.xiguaji.boot.toolkit.JsonUtil;
//import com.xiguaji.tiktok.task.market.model.BloggerMarket;
//import com.xiguaji.tiktok.task.market.model.Pagination;
//import com.xiguaji.tiktok.task.market.model.author.Author;
//import com.xiguaji.tiktok.task.market.model.author.MarketAuthorResponse;
//import com.xiguaji.tiktok.task.market.model.distribution.Property;
//import com.xiguaji.tiktok.task.market.repository.BloggerMarketRepository;
//import com.xiguaji.tiktok.task.market.spider.MarketSpiderClient;
//import lombok.val;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.Optional;
//
//@Component
//public class FetchBloggerMarketDistributionEventListener implements IEventListener<FetchBloggerMarketDistributionEventListener.FetchBloggerMarketDistributionEvent> {
//
//    @Autowired
//    MarketSpiderClient marketSpiderClient;
//
//    @Autowired
//    BloggerMarketRepository bloggerMarketRepository;
//
//    @Override
//    @Subscribe
//    @AllowConcurrentEvents
//    public void handle(FetchBloggerMarketDistributionEvent event) {
//        BloggerMarket bloggerMarket = event.bloggerMarket;
//        if(null == bloggerMarket){
//            bloggerMarket = bloggerMarketRepository.findByMarketId(event.marketId);
//            if(null == bloggerMarket){
//                return;
//            }
//        }
//        val distribution = marketSpiderClient.getDistribution(bloggerMarket.getMarketId());
//        for (Property property : distribution.getData().getActive()) {
//            if(property.getProperty().equals("active")){
//                bloggerMarket.setActive(property.getValue());
//            }else if(property.getProperty().equals("inactive")){
//                bloggerMarket.setInactive(property.getValue());
//            }
//        }
//        bloggerMarket.setAgeDistribution(JsonUtil.parseJson(distribution.getData().getAge()));
//        bloggerMarket.setGenderDistribution(JsonUtil.parseJson(distribution.getData().getGender()));
//        bloggerMarket.setRegionDistribution(JsonUtil.parseJson(distribution.getData().getRegion()));
//        bloggerMarket.setRegionGeonameidDistribution(JsonUtil.parseJson(distribution.getData().getRegion_geonameid()));
//        bloggerMarket.setStateDistribution(JsonUtil.parseJson(distribution.getData().getState()));
//        bloggerMarket.setStateGeonameidDistribution(JsonUtil.parseJson(distribution.getData().getState_geonameid()));
//        bloggerMarket.setUpdateBy(event.getSource());
//        bloggerMarketRepository.save(bloggerMarket);
//    }
//
//    public static class FetchBloggerMarketDistributionEvent extends Event{
//        private long marketId;
//        private BloggerMarket bloggerMarket;
//
//        public FetchBloggerMarketDistributionEvent(Event event,long marketId) {
//            super(event,"FetchBloggerMarketDistributionEventListener");
//            this.marketId = marketId;
//        }
//
//        public FetchBloggerMarketDistributionEvent(Event event,BloggerMarket bloggerMarket) {
//            super(event,"FetchBloggerMarketDistributionEventListener");
//            this.bloggerMarket = bloggerMarket;
//        }
//        public FetchBloggerMarketDistributionEvent(BloggerMarket bloggerMarket) {
//            super("FetchBloggerMarketDistributionEventListener");
//            this.bloggerMarket = bloggerMarket;
//        }
//
//        public FetchBloggerMarketDistributionEvent(long marketId) {
//            super("FetchBloggerMarketDistributionEventListener");
//            this.marketId = marketId;
//        }
//    }
//}
